package com.bookheaven.beans;

import java.io.Serializable;
import java.util.Locale;

import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

@Named("loginBean")
@SessionScoped
public class LoginBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7608018230995089570L;
	private String email = null;
	private String message = null;
	private String password = null;
	private boolean isEng = true;

	private boolean isLogin = false;
	private final String loginSql = "SELECT CAST(u.password = ? AS BOOLEAN) AS login "
			+ "FROM app_user u where u.email = ? ";

	public String CheckValidUser() {
		// // FacesContext context = FacesContext.getCurrentInstance();
		// // dataProcessor.connectToDB();
		// // preparedStatement = dataProcessor.getPreparedStatement(loginSql);
		// try {
		// preparedStatement.setString(1, password);
		//
		// preparedStatement.setString(2, email);
		// ResultSet loginSet = preparedStatement.executeQuery();
		// if (loginSet.next() && loginSet.getBoolean("login")) {
		// context.addMessage("success",
		// new FacesMessage("Hello " + email));
		// isLogin = true;
		// return "success";
		// }
		// } catch (SQLException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// isLogin = false;
		// context.addMessage("error", new FacesMessage(
		// "User name or password fault!"));
		return "fail";
	}

	public String logout() {
		isLogin = false;
		cancel();
		return "logout";
	}

	public void cancel() {
		message = null;
		email = null;
		password = null;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public boolean isLogin() {
		return isLogin;
	}

	public void setLogin(boolean isLogin) {
		this.isLogin = isLogin;
	}

	public void changeLocale() {
		if (isEng) {
			FacesContext.getCurrentInstance().getViewRoot()
					.setLocale(new Locale("tr", "TR"));
			isEng = false;
		} else {
			FacesContext.getCurrentInstance().getViewRoot()
					.setLocale(new Locale("en", "US"));
			isEng = true;
		}
	}

	public boolean isEng() {
		return isEng;
	}

	public void setEng(boolean isEng) {
		this.isEng = isEng;
	}

}
